<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'

require('./spider') // echarts theme
import {debounce} from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    optionTitleText: {
      type: String,
      default: ''
    },
    opinionYAxisData: {
      type: Array,
      default: []
    },
    opinionVisualMapMax: {
      type: Number,
      default: 10
    },
    opinionSeriesData: {
      type: Array,
      default: []
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      opinion: {
        title: {
          text: this.optionTitleText,
          padding: 10,
        },

        backgroundColor: '#fff',
        tooltip: {
          position: 'top'
        },
        animation: false,
        grid: {
          top: 30,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00',
            '07:00', '08:00', '09:00', '10:00', '11:00',
            '12:00', '13:00', '14:00', '15:00', '16:00', '17:00',
            '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'],
          splitArea: {
            show: true
          }
        },
        yAxis: {
          type: 'category',
          data: this.opinionYAxisData,
          splitArea: {
            show: true
          }
        },
        visualMap: {
          min: 0,
          max: this.opinionVisualMapMax,
          calculable: true,
          orient: 'horizontal',
          left: 'center',
          bottom: '15%'
        },
        series: [{
          name: '数量',
          type: 'heatmap',
          data: this.opinionSeriesData,
          label: {
            normal: {
              show: true
            }
          },
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }]
      }
    }
  },
  watch: {
    optionTitleText(newValue, oldValue) {
      console.log("oldValue", JSON.stringify(oldValue))
      console.log("newValue", JSON.stringify(newValue))
      this.opinion.title.text = newValue
      this.chart.setOption(this.opinion);
    },
    opinionYAxisData(newValue, oldValue) {
      console.log("oldValue", JSON.stringify(oldValue))
      console.log("newValue", JSON.stringify(newValue))
      this.opinion.yAxis.data = newValue
      this.chart.setOption(this.opinion);
    },
    opinionSeriesData(newValue, oldValue) {
      console.log("oldValue", JSON.stringify(oldValue))
      console.log("newValue", JSON.stringify(newValue))
      this.opinion.series[0].data = newValue
      this.chart.setOption(this.opinion);
    },
    opinionVisualMapMax(newValue, oldValue) {
      console.log("oldValue", JSON.stringify(oldValue))
      console.log("newValue", JSON.stringify(newValue))
      this.opinion.visualMap.max = newValue
      this.chart.setOption(this.opinion);
    },
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'spider')
      this.chart.setOption(this.opinion)
    }
  }

}
</script>
